package com.xzj.admin.analysis.base.common.handle;

import cn.hutool.extra.spring.SpringUtil;
import com.xzj.admin.analysis.base.common.redis.ICustomRedisHandle;
import com.xzj.admin.analysis.base.constants.CommonEnums;
import com.xzj.admin.analysis.base.domain.ResMessageManage;
import com.xzj.admin.analysis.base.domain.CommonFlowInfo;
import com.xzj.admin.analysis.base.util.AnalysisCommonUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * @program: xzj-admin
 * @ClassName: ThemeSpecialAnalysisHandle
 * @description: 资源主题特殊处理类
 * @author: XiaZunJun
 * @create: 2024-11-13 00:36
 */
@Getter
@Slf4j
public abstract class ThemeSpecialAnalysisHandle implements CommonEnums {

    private final String cloudId;

    private final String cloudType;

    protected static final ICustomRedisHandle<String> redisHandle = SpringUtil.getBean(ICustomRedisHandle.class);

    private final Function<ResMessageManage,Boolean> specialHandleFun;

    private static final Map<String, BiConsumer<ResMessageManage,CommonFlowInfo>> SPECIAL_HANDLE_MAP = new ConcurrentHashMap<>();

    private static final Map<String, BiConsumer<ResMessageManage,CommonFlowInfo>> THEME_SPECIAL_HANDLE_MAP = new ConcurrentHashMap<>();

    protected static void putThemeSpecialHandle(String theme, BiConsumer<ResMessageManage,CommonFlowInfo> handle) {
        THEME_SPECIAL_HANDLE_MAP.put(theme,handle);
    }

    protected static void executeThemeSpecialHandle(String theme, ResMessageManage message, CommonFlowInfo info){
        if(THEME_SPECIAL_HANDLE_MAP.containsKey(theme)){
            THEME_SPECIAL_HANDLE_MAP.get(theme).accept(message,info);
        }
    }

    public ThemeSpecialAnalysisHandle(String cloudId, String cloudType,Function<ResMessageManage,Boolean> specialHandleFun) {
        this.cloudId = cloudId;
        this.cloudType = cloudType;
        this.specialHandleFun = specialHandleFun;
        SPECIAL_HANDLE_MAP.put(MESSAGE_TAG.add.name(),this::add);
        SPECIAL_HANDLE_MAP.put(MESSAGE_TAG.edit.name(),this::edit);
        SPECIAL_HANDLE_MAP.put(MESSAGE_TAG.del.name(),this::del);
    }

    public void handle(ResMessageManage message, CommonFlowInfo info){
        if(specialHandleFun.apply(message)){
            if(SPECIAL_HANDLE_MAP.containsKey(message.getMessageTag())){
                SPECIAL_HANDLE_MAP.get(message.getMessageTag()).accept(message,info);
            }
        }
    }

    protected void add(ResMessageManage message, CommonFlowInfo info){
        String handleKey = buildKey(message.getMessageSource(),message.getMessageTag());
        executeThemeSpecialHandle(handleKey,message,info);
    };

    protected void edit(ResMessageManage message, CommonFlowInfo info){
        String handleKey = buildKey(message.getMessageSource(),message.getMessageTag());
        executeThemeSpecialHandle(handleKey,message,info);
    };

    protected void del(ResMessageManage message, CommonFlowInfo info){
        String handleKey = buildKey(message.getMessageSource(),message.getMessageTag());
        executeThemeSpecialHandle(handleKey,message,info);
    };

    protected static String buildKey(String... args){
        return AnalysisCommonUtil.commonBuildStr(args);
    }

}
